package com.ydc.ln.tutorial.web.tag;

import java.io.IOException;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.ydc.ln.commons.utils.page.Pagination;

/**
 * 分页标签处理类
 * 
 * @author yangdc
 * 
 */
public class SimplePaginationTag<T> extends SimpleTagSupport {
	/**
	 * 分页数据(required)
	 */
	private Pagination<T> pagination;
	/**
	 * 查询form的Id,默认为queryForm
	 */
	private String queryForm = "queryForm";
	/**
	 * 分页div ID,默认为paginationId
	 */
	private String divId = "paginationId";
	/**
	 * 分页样式,默认为technorati
	 */
	private String divClass = "technorati";
	/**
	 * 数据查询URL,类似WordServlet?method=list&pageNo=444
	 */
	private String queryUrl = "";

	public void doTag() throws JspException, IOException {
		// 参数验证

		String strPre = "< pre";
		String strNext = "next >";
		String nextLine = "\n";

		StringBuffer strBuf = new StringBuffer();

		// strBuf.append("<input type='hidden' name='pageNo'/>").append(nextLine);

		/** script start */
		// strBuf.append("<script type=\"text/javascript\">").append(nextLine);
		// strBuf.append("var paga_queryForm = document.getElementById('").append(queryForm).append("');").append(nextLine);
		// strBuf.append("function paga_toPage(pageNo) {").append(nextLine);
		// strBuf.append("paga_queryForm.pageNo.value=").append("pageNo;").append(nextLine);
		// strBuf.append("paga_queryForm.submit();").append(nextLine);
		// strBuf.append("}").append(nextLine);
		// strBuf.append("</script>").append(nextLine);
		/** script end */

		// 具体格式 上一页1...具体页数...n下一页(第n/m页 共xx条)
		strBuf.append("<div id=\"").append(divId).append("\" class=\"").append(divClass).append("\">").append(nextLine);

		// 上一页
		if (pagination.getPageNo() == 1) {
			strBuf.append("<span class=\"disabled\">").append(strPre).append(" </span> ").append(nextLine);
		} else {
			strBuf.append("<a href=\"#\" onclick=\"paga_toPage(").append(pagination.getPageNo() - 1).append(")\">").append(strPre).append(" </a> ").append(nextLine);
		}
		// ...
		if (pagination.getStartIndex() > 1) {
			strBuf.append("<a href=\"#\" onclick=\"paga_toPage(").append(1).append(");\">").append(1).append("</a>");
			strBuf.append(" ... ").append(nextLine);
		}
		// 具体页数
		for (int i = pagination.getStartIndex(); i <= pagination.getEndIndex(); i++) {
			if (pagination.getPageNo() == i) {
				strBuf.append("<span class=\"current\">").append(i).append("</span>").append(nextLine);
			} else {
				strBuf.append("<a href=\"#\" onclick=\"paga_toPage(").append(i).append(");\">").append(i).append("</a>").append(nextLine);
			}
		}
		// ...
		if (pagination.getEndIndex() < pagination.getTotalPage()) {
			strBuf.append(" ... ");
			strBuf.append("<a href=\"#\" onclick=\"paga_toPage(").append(pagination.getTotalPage()).append(");\">").append(pagination.getTotalPage()).append("</a>").append(nextLine);
		}

		// 下一页
		if (pagination.getPageNo() == pagination.getTotalPage()) {
			strBuf.append("<span class=\"disabled\">").append(strNext).append(" </span> ").append(nextLine);
		} else {
			strBuf.append("<a href=\"#\" onclick=\"paga_toPage(").append(pagination.getPageNo() + 1).append(")\">").append(strNext).append(" </a> ").append(nextLine);
		}
		// 分页信息(第n/m页 共xx条)
		strBuf.append("<span class=\"info\">(第").append(pagination.getPageNo()).append("/").append(pagination.getTotalPage()).append("页").append(" 共").append(pagination.getTotalRecord()).append(
				"条)</span>").append(nextLine);

		strBuf.append("</div>").append(nextLine);
		// System.out.println(strBuf.toString());
		JspContext ctx = getJspContext();
		JspWriter out = ctx.getOut();
		out.print(strBuf.toString());
	}

	public Pagination<T> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<T> pagination) {
		this.pagination = pagination;
	}

	public String getQueryForm() {
		return queryForm;
	}

	public void setQueryForm(String queryForm) {
		this.queryForm = queryForm;
	}

	public String getDivId() {
		return divId;
	}

	public void setDivId(String divId) {
		this.divId = divId;
	}

	public String getDivClass() {
		return divClass;
	}

	public void setDivClass(String divClass) {
		this.divClass = divClass;
	}
}
